package lib.toolbar.activity;


import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ViewSwitcher;

import hd.util.ScreenHelper;
import hd.util.activity.BaseActivity;
import hd.util.widget.TitleBarShadowView;
import lib.toolbar.R;
import lib.toolbar.widget.ToolBarView;

/**
 * Base ToolBar Activity
 * <p/>
 * Created by Haidy on 15/3/19.
 */
public class BaseToolBarActivity extends BaseActivity {

    private ToolBarView toolBarView;
    private ViewSwitcher switcher;
    private View body;
    private int toolBarHeight;
    private boolean isToolbarShowing = true;
    private TitleBarShadowView toolbarShadow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hindActionBar();
        setContentView(view = View.inflate(this, R.layout.tool_bar_activity_view, null));

        toolBarView = (ToolBarView) findViewById(R.id.toolbar);
        switcher = (ViewSwitcher) findViewById(R.id.switcher);
        toolbarShadow = (TitleBarShadowView) findViewById(R.id.toolbar_shadow);

        toolBarHeight = this.getResources().getDimensionPixelOffset(R.dimen.titleBarHeight);
    }

    /**
     * 设置Toolbar
     *
     * @param layout layout
     */
    public void setToolBar(int layout) {
        setToolBar((ToolBarView) View.inflate(this, layout, null));
    }

    /**
     * 设置Toolbar
     *
     * @param toolBarView view
     */
    public void setToolBar(ToolBarView toolBarView) {
        LinearLayout viewGroup = (LinearLayout) view;
        viewGroup.removeView(this.toolBarView);
        toolBarView.setLayoutParams(new ToolBarView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, toolBarHeight));
        viewGroup.addView(this.toolBarView = toolBarView, 0);
    }

    /**
     * 设置Toolbar的高度
     *
     * @param toolBarHeight height
     */
    public void setToolBarHeight(int toolBarHeight) {
        this.toolBarHeight = toolBarHeight;
        ViewGroup.LayoutParams lp = toolBarView.getLayoutParams();
        lp.height = toolBarHeight;
        toolBarView.setLayoutParams(lp);
    }

    /**
     * 获取Toolbar的高度
     *
     * @return 高度
     */
    public int getToolBarHeight() {
        return toolBarHeight;
    }

    /**
     * 设置Body View
     *
     * @param layout layout
     */
    public void setBody(int layout) {
        setBody(View.inflate(this, layout, null));
    }

    /**
     * 设置BodyView
     *
     * @param body view
     */
    public void setBody(View body) {
        if (this.body != null) {
            switcher.removeView(this.body);
        }
        switcher.addView(this.body = body, 0);
    }

    /**
     * 获取tool bar view
     *
     * @return tool bar
     */
    public ToolBarView getToolBar() {
        return toolBarView;
    }

    /**
     * 隐藏ToolBar
     */
    public void hindToolBar() {
        if (isToolbarShowing) {
            getToolBar().setVisibility(View.GONE);
            this.toolbarShadow.setVisibility(View.GONE);
            isToolbarShowing = false;
        }
    }

    /**
     * 显示ToolBar
     */
    public void showToolBar() {
        if (!isToolbarShowing) {
            getToolBar().setVisibility(View.VISIBLE);
            this.toolbarShadow.setVisibility(View.VISIBLE);
            isToolbarShowing = true;
        }
    }

    public View getBody() {
        return body;
    }
}
